<template>
	<!--小区设备列表-->
	<view class="community">
		<view class="head_bg" style="position: relative;">
			<view class="ele_title_left padding-left-sm padding-bottom-xs">
				<view class="ele_title_inner padding-xs padding-left-sm" style="color:white">
				<image class="ele_address_img" src="../../../common/img/gongsi_bai.png" mode=""></image>
				{{info.residential_quarters_name}}</view>
				<view class="ele_address padding-xs padding-left-sm">
					<image class="ele_address_img" src="../../../common/img/dizhi_bai.png" mode=""></image>
					<text class="text-white">{{info.residential_quarters_address}}</text>
				</view>
			</view>
			<view class="add_equ" @tap="addEqu">+新增</view>
		</view>
		<view class="big_box" style="top: -80rpx;">
			<view class="count_tab">
				<view :class="{count_tab_inner1: istrue == true,is_active: tabIndex == 1}" @tap="tabSelect" :data-id="1" style="margin-left: 67rpx;">全部</view>
				<view :class="{count_tab_inner1: istrue == true,is_active: tabIndex == 2}" @tap="tabSelect" :data-id="2" >正常</view>
				<!-- <view :class="{count_tab_inner1: istrue == true,is_active: tabIndex == 3}" @tap="tabSelect" :data-id="1" >使用中</view> -->
				<view :class="{count_tab_inner1: istrue == true,is_active: tabIndex == 4}" @tap="tabSelect" :data-id="4" style="margin-right: 67rpx;">故障</view>
			</view>
			<view v-if="tabIndex == 1">
				<view class="equ_list" v-if="list.length>0" v-for="(info,index) in list" :key="index" >
					<view class="equ_list_title">
						<view class="equ_list_title_left">
							<text>设备编号：{{info.equipment_number}}</text>
							<text v-if="info.equipment_state==0" class="green_color">[正常]</text>
							<text v-if="info.equipment_state==1" class="orange_color">[故障]</text>
						</view>
						<image @tap="del" class="equ_list_title_img" src="../../../common/img/yichu.png" mode=""></image>
					</view>
					<view class="equ_list_inner">
						<view class="flex text-center padding-top-xs">
							<view class="flex-sub">
								<view v-if="info.online_status==0" class="text-green">离线</view>
								<view v-if="info.online_status==1" class="text-green">在线</view>
								<view>在线状态</view>
							</view>
							<view class="flex-sub">
								<view v-if="info.left_socket_status==0">空闲</view>
								<view v-if="info.left_socket_status==1">使用中</view>
								<view>左边插座</view>
							</view>
							<view class="flex-sub">
								<view v-if="info.right_socket_status==0">空闲</view>
								<view v-if="info.right_socket_status==1">使用中</view>
								<view>右边插座</view>
							</view>
						</view>
						<view class="equ_list_inner_text">网关编号：{{info.gateway_sn}}</view>
						<view class="equ_list_inner_text">起止时间：{{info.add_time}} 至 {{info.end_time}}</view>
					</view>
				</view>
				<view style="height:100rpx;"></view>
				<view v-if="list.length==0" class="margin-top-lg text-center">
					<image src='../../../common/img/no_data.png' style="width:275rpx;height:312rpx;"></image>
				</view>
				<view v-if="list.length>0" class="text-black text-df text-center bg-white padding-sm padding-left-lg be-gray">
					{{loadingTxt}} 共({{list.length}}条记录)
				</view>
			</view>
			<view v-if="tabIndex == 2">
				<view class="equ_list" v-if="list.length>0" v-for="(info,index) in list" :key="index" >
					<view class="equ_list_title">
						<view class="equ_list_title_left">
							<text>设备编号：{{info.equipment_number}}</text>
							<text v-if="info.equipment_state==0" class="green_color">[正常]</text>
							<text v-if="info.equipment_state==1" class="orange_color">[故障]</text>
						</view>
						<image @tap="del" class="equ_list_title_img" src="../../../common/img/yichu.png" mode=""></image>
					</view>
					<view class="equ_list_inner">
						<view class="flex text-center padding-top-xs">
							<view class="flex-sub">
								<view v-if="info.online_status==0" class="text-green">离线</view>
								<view v-if="info.online_status==1" class="text-green">在线</view>
								<view>在线状态</view>
							</view>
							<view class="flex-sub">
								<view v-if="info.left_socket_status==0">空闲</view>
								<view v-if="info.left_socket_status==1">使用中</view>
								<view>左边插座</view>
							</view>
							<view class="flex-sub">
								<view v-if="info.right_socket_status==0">空闲</view>
								<view v-if="info.right_socket_status==1">使用中</view>
								<view>右边插座</view>
							</view>
						</view>
						<view class="equ_list_inner_text">网关编号：{{info.gateway_sn}}</view>
						<view class="equ_list_inner_text">起止时间：{{info.add_time}} 至 {{info.end_time}}</view>
					</view>
				</view>
				<view style="height:100rpx;"></view>
				<view v-if="list.length==0" class="margin-top-lg text-center">
					<image src='../../../common/img/no_data.png' style="width:275rpx;height:312rpx;"></image>
				</view>
				<view v-if="list.length>0" class="text-black text-df text-center bg-white padding-sm padding-left-lg be-gray">
					{{loadingTxt}} 共({{list.length}}条记录)
				</view>
			</view>
			<view v-if="tabIndex == 4">
				<view class="equ_list" v-if="list.length>0" v-for="(info,index) in list" :key="index" >
					<view class="equ_list_title">
						<view class="equ_list_title_left">
							<text>设备编号：{{info.equipment_number}}</text>
							<text v-if="info.equipment_state==0" class="green_color">[正常]</text>
							<text v-if="info.equipment_state==1" class="orange_color">[故障]</text>
						</view>
						<image @tap="del(info.equipment_number)" class="equ_list_title_img" src="../../../common/img/yichu.png" mode=""></image>
					</view>
					<view class="equ_list_inner">
						<view class="flex text-center padding-top-xs">
							<view class="flex-sub">
								<view v-if="info.online_status==0" class="text-green">离线</view>
								<view v-if="info.online_status==1" class="text-green">在线</view>
								<view>在线状态</view>
							</view>
							<view class="flex-sub">
								<view v-if="info.left_socket_status==0">空闲</view>
								<view v-if="info.left_socket_status==1">使用中</view>
								<view>左边插座</view>
							</view>
							<view class="flex-sub">
								<view v-if="info.right_socket_status==0">空闲</view>
								<view v-if="info.right_socket_status==1">使用中</view>
								<view>右边插座</view>
							</view>
						</view>
						<view class="equ_list_inner_text">网关编号：{{info.gateway_sn}}</view>
						<view class="equ_list_inner_text">起止时间：{{info.add_time}} 至 {{info.end_time}}</view>
					</view>
				</view>
				<view style="height:100rpx;"></view>
				<view v-if="list.length==0" class="margin-top-lg text-center">
					<image src='../../../common/img/no_data.png' style="width:275rpx;height:312rpx;"></image>
				</view>
				<view v-if="list.length>0" class="text-black text-df text-center bg-white padding-sm padding-left-lg be-gray">
					{{loadingTxt}} 共({{list.length}}条记录)
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	let page=1,timer=null
	export default {
		data() {
			return {
				istrue:true,
				tabIndex: 1,
				list:[],
				loadingTxt:'加载更多',
				total:0,
				info:''
			}
		},
		onLoad(options) {
			let info=JSON.parse(options.item);
			console.log("info="+info);
			this.info=info;
		},
		onShow() {
			page=1;
			this.getalllist();
		},
		methods: {
			tabSelect(e) {
				page=1;
				this.tabIndex = e.currentTarget.dataset.id;
				this.list=[];
				this.loadingTxt='加载更多',
				this.getalllist();
			},
			addEqu(){
				uni.navigateTo({
					url: '/pages/equipment/addequipment/addequipment?item='+JSON.stringify(this.info)
				})
			},
			getalllist(){
				let that=this;
				let type;
				if(this.tabIndex==1){
					type="0";
				}else if(this.tabIndex==2){
					type="1";
				}else if(this.tabIndex==4){
					type="2";
				}
				that.http.tokenreq({
					url: "devicemanagement/v1/list",
					data:{
						'pageindex':page,
						"residential_quarters_guid":this.info.residential_quarters_guid,
						"equipment_state":type
					}
				}).then((res) => {
					let e=res.data;
					console.log(e)
					if(e.code==0 && e.success){
						that.list=e.rows;
						page++
					}
				})
			},
			getmorelist(){
				this.loadingTxt='加载中'
				let that=this;
				let type;
				if(this.tabIndex==1){
					type="0";
				}else if(this.tabIndex==2){
					type="1";
				}else if(this.tabIndex==4){
					type="2";
				}
				that.http.tokenreq({
					url: "devicemanagement/v1/list",
					data:{
						'pageindex':page,
						"residential_quarters_guid":this.info.residential_quarters_guid,
						"equipment_state":type
					}
				}).then((res) => {
					uni.hideLoading();
					let e=res.data;
					if(e.code==0 && e.success){
						if(e.rows.length==0){
							that.loadingTxt="已经加载全部";
							return;
						}
						that.list=that.list.concat(e.rows);
						page++;
					}
				})
			},
			onReachBottom() {
				//阻止重复加载
				if(timer !== null){
					clearTimeout(timer)
				}
				timer=setTimeout(()=>this.getmorelist(),500)
			},
			/* toEquDetail(){
				uni.navigateTo({
					url: '../equipmentDetail/equipmentDetail'
				})
			}, */
			//删除设备
			del(number){
				let that=this;
				uni.showModal({
					title: '提示',
					content: '确定要删除该设备信息吗?',
					success: function (res) {
						if (res.confirm) {
							that.suredel(number);
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			suredel(number){
				let that=this;
				uni.showLoading();
				that.http.tokenreq({
					url: 'devicemanagement/v1/del',
					data:{"equipment_number":number}
				}).then((res) => {
					console.log("res="+JSON.stringify(res));
					let e=res.data;
					if(e.code==0 && e.success){
						uni.hideLoading();
						uni.showModal({
							content: e.message,
							showCancel:false,
							success: function (res) {
								if (res.confirm) {
									that.getalllist()
								}
							}
						});
					}else{
						uni.showModal({
							content:e.message,
							showCancel:false,
						})
					}
				})
			}
		}
	}
</script>
